<?php
namespace app\admin\controller;

use app\model\Goods_brands;
use app\model\Goods_cates;
use app\model\GoodsModel;

class Goods {

//请参考 81_电商数据库.sql，将其导入到mysql数据库，使用thinkphp模型做如下练习：
//
//参考 82_电商数据.md，系统增加了品牌、分类和商品，将其中相关数据插入到对应表。
//r510vc 15.6英寸笔记本 已经售罄，请修改相关数据。
//15.6 寸电脑屏保护膜 下架处理，请修改相关数据。
//查询所有的商品、品牌和分类数据。
    public function seleteModel(){
        //商品查询
//        $good = GoodsModel::select();
//        foreach ($good as $item){
//            echo $item['name'] |;
//            echo "<br />";
//            echo $item['cate_id'];
//            echo "<br />";
//            echo $item['brand_id'];
//            echo "<br />";
//            echo $item['price'];
//            echo "<br />";
//            echo $item['is_show'];
//            echo "<br />";
//            echo $item['is_saleoff'];
//            echo "<br />";
//            echo "<br />";
//        }
        //品牌查询
//        $goods = Goods_brands::select();
//        foreach($goods as $itme){
//            echo $itme['id'];
//            echo "<br />";
//            echo $itme['name'];
//            echo "<br />";
//            echo "<br />";
//        }

        //分类数据
        $goods = Goods_cates::select();
        foreach($goods as $itme){
            echo $itme['id'];
            echo "<br />";
            echo $itme['name'];
            echo "<br />";
            echo "<br />";
        }
    }
//查询所有的商品，并且按照价格降序排列。
    public function desc(){
        $goods = GoodsModel::order('price','desc')->select();
        foreach ($goods as $item){
            echo $item['id'];
            echo "<br />";
            echo $item['name'] ;
            echo "<br />";
            echo $item['cate_id'];
            echo "<br />";
            echo $item['brand_id'];
            echo "<br />";
            echo $item['price'];
            echo "<br />";
            echo $item['is_show'];
            echo "<br />";
            echo $item['is_saleoff'];
            echo "<br />";
            echo "<br />";
        }
    }
//查询出品牌是戴尔的商品。
    public function link(){
        $goods = \think\facade\Db::query("SELECT * FROM goods WHERE brand_id = (SELECT id FROM goods_brands WHERE name = '戴尔')");

        foreach ($goods as $item){
            echo $item['id'];
            echo "<br />";
            echo $item['name'] ;
            echo "<br />";
            echo $item['cate_id'];
            echo "<br />";
            echo $item['brand_id'];
            echo "<br />";
            echo $item['price'];
            echo "<br />";
            echo $item['is_show'];
            echo "<br />";
            echo $item['is_saleoff'];
            echo "<br />";
            echo "<br />";
        }

}
//查询出所有超极本的商品。
    public function cates(){
//        $goods = Goods_cates::where('name','超级本')->select();
        $goods = \think\facade\Db::query("SELECT * FROM goods WHERE cate_id = (SELECT id FROM goods_cates WHERE name = '超极本')");
        foreach ($goods as $item){
            echo $item['id'];
            echo "<br />";
            echo $item['name'] ;
            echo "<br />";
            echo $item['cate_id'];
            echo "<br />";
            echo $item['brand_id'];
            echo "<br />";
            echo $item['price'];
            echo "<br />";
            echo $item['is_show'];
            echo "<br />";
            echo $item['is_saleoff'];
            echo "<br />";
            echo "<br />";
        }
    }
//查询华硕品牌的超极本。
    public function name(){
        $goods = \think\facade\Db::query("SELECT * FROM goods WHERE brand_id = (SELECT id FROM goods_brands WHERE name = '华硕') AND name LIKE '%笔记本%'");
        foreach ($goods as $item){
            echo $item['id'];
            echo "<br />";
            echo $item['name'] ;
            echo "<br />";
            echo $item['cate_id'];
            echo "<br />";
            echo $item['brand_id'];
            echo "<br />";
            echo $item['price'];
            echo "<br />";
            echo $item['is_show'];
            echo "<br />";
            echo $item['is_saleoff'];
            echo "<br />";
            echo "<br />";
        }
    }
//查询出价格最高的商品和最低的商品。
    public function max(){
        $goods = \think\facade\Db::query("SELECT name, MAX(price) FROM goods ");
        var_dump($goods);
    }
//查询是否有售罄的商品。
    public function saleoff(){
        $goods = GoodsModel::where('is_saleoff','=','0')->select();
        foreach ($goods as $item){
            echo $item['id'];
            echo "<br />";
            echo $item['name'] ;
            echo "<br />";
            echo $item['cate_id'];
            echo "<br />";
            echo $item['brand_id'];
            echo "<br />";
            echo $item['price'];
            echo "<br />";
            echo $item['is_show'];
            echo "<br />";
            echo $item['is_saleoff'];
            echo "<br />";
            echo "<br />";
        }
    }
//查询所有商品、品牌和分类总数。
    public function count(){
        //商品
//        $goods = GoodsModel::count();
        //品牌
//        $goods = Goods_brands::count();
        //分类总数
        $goods = Goods_cates::count();
        var_dump($goods);

    }
//每页显示10件商品，查询出第1页和第2页的数据。
    public function limit(){
        $goods = \think\facade\Db::query("SELECT * FROM goods WHERE id >0 LIMIT 2");
        foreach ($goods as $item){
            echo $item['id'];
            echo "<br />";
            echo $item['name'] ;
            echo "<br />";
            echo $item['cate_id'];
            echo "<br />";
            echo $item['brand_id'];
            echo "<br />";
            echo $item['price'];
            echo "<br />";
            echo $item['is_show'];
            echo "<br />";
            echo $item['is_saleoff'];
            echo "<br />";
            echo "<br />";
        }
    }
}